// 用户所在主体 - system / hotel
import { DataTypes, UUIDV4 } from 'sequelize';
import { MySqlModel, getSequelize } from '../db';

class UserHotels extends MySqlModel {

}

UserHotels.init({
  id: {
    type: DataTypes.UUID, unique: 'id', allowNull: false, defaultValue: UUIDV4, validate: { isUUID: 4 }, primaryKey: true, comment: '主键', editable: false
  },
  user_id: {
    type: DataTypes.UUID, allowNull: false, unique: 'user_hotel_id', validate: { isUUID: 4 }, comment: '用户', editable: false
  },
  hotel_id: {
    type: DataTypes.UUID, allowNull: false, unique: 'user_hotel_id', validate: { isUUID: 4 }, comment: '组织', editable: false
  },
  is_admin: {
    type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, comment: '主体管理员'
  },
  created_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '创建用户', editable: false
  },
  created_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '更新时间'
  }
}, {
  sequelize: getSequelize(),
  tableName: 'user_hotels',
  comment: '用户主体表'
});

// UserHotels.associate = (models) => {
//   UserHotels.belongsTo(models.Country, {
//     targetKey: 'id',
//     foreignKey: 'country_id',
//     constraints: false,
//     as: 'country'
//   });
// };

UserHotels.afterDestroy(async (instance, options) => {
  const { transaction } = options;
  await getSequelize().models.UserPermissions.destroy({
    transaction,
    where: {
      user_id: instance.user_id,
      hotel_id: instance.hotel_id
    },

    individualHooks: true
  });
});

export default UserHotels;
